﻿#include "pch.h"
#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

class Solution
{
public:
	vector<int> twoSum(vector<int>& nums, int target)
	{
		unordered_map<int, int> hashtable;
		for (int i = 0;i < nums.size();++i)
		{
			//遍历输出hashtable
			for (auto ix = hashtable.begin();ix !=hashtable.end();++ix)
			{
				cout << ix->first<<":"<<ix->second << endl;
			}
			//查找hashtable中有没有目标元素
			auto it = hashtable.find(target - nums[i]);
			
			if (it != hashtable.end()) 
			{
				//找到目标元素
				return { it->second,i };
			}
			//初始化hashtable
			hashtable[nums[i]] = i;
			cout << "***" << endl;
		}
		return {};
	}
};

int main()
{
	Solution s;
	vector<int> arr1 { 2,11,7,15 };
	for (auto it = arr1.begin();it < arr1.end();it++)
	{
		cout << (*it) << endl;
	}

	vector<int> arr2 = s.twoSum(arr1, 9);
	for (auto it = arr2.begin();it < arr2.end();it++)
	{
		cout << (*it) << endl;
	}
	
	return 0;
}